package com.guangyuan.network.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guangyuan.network.datasource.pool.util.ConnectionUtil;
import com.guangyuan.network.mvc.action.Action;

public class TableMetaAction implements Action{
	
	public boolean update(HttpServletRequest request, HttpServletResponse response) {
		String tableName = request.getParameter("tableName");
		
		TableMeta tableMeta = CreateSql.getTableMeta(tableName);
		String[]values = request.getParameterValues("value");
		for(int i = 0; i < values.length; i ++) {
			System.out.println(tableMeta.getHeaders().get(i) + " -- " + values[i]);
			
		}
		Connection conn = ConnectionUtil.getConnection();
		try {
			PreparedStatement pstmt = conn.prepareStatement(tableMeta.getUpdateSql());
			for(int i = 0; i < values.length; i ++) {
				pstmt.setString(i + 1, values[i]);
			}
			System.out.println(tableMeta.getUpdateSql());
			pstmt.setString(values.length + 1, values[0]);
			pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionUtil.closeConnection(conn);
		}
		
		
		return false;
	}
	
}
